import React, { Component } from 'react'
import request from '../util/request'

export default class EditPwd extends Component {
    state = {
        oldPwd : '',
        newPwd : ''
    }
    render() {
        let {oldPwd,newPwd} = this.state;
        return (
            <div>
                原密码： <input type="text" placeholder="原密码" value={oldPwd} name="oldPwd" onChange={this.change}/>
                新密码： <input type="text" placeholder="新密码" value={newPwd} name="newPwd"  onChange={this.change}/>
                <button onClick={this.edit}>确认修改</button>
            </div>
        )
    }

    //受控表单
    change = (e) => {
        this.setState({
            [e.target.name]:e.target.value
        })
    }

    //确认修改
    edit = () => {
        let {oldPwd,newPwd} = this.state;
        let id = localStorage.getItem('id');
        request.put('/api/editPwd',{oldPwd,newPwd,id}).then(res => {
            console.log(res);
            if(res.data.code === 1){
                this.props.history.push('/login');
            }else{
                alert('修改失败')
            }
        })
    }

}
